package com.example.heathy_me.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.example.heathy_me.R;
import com.example.heathy_me.homeDetail.Medicine;

public class FriendControlAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //你的逻辑处理
        //如果需要实现间隔定时器功能，在重新执行1的发送步骤，实现间隔定时，间隔时间为INTERVAL
        //Toast.makeText(context ,"getet", Toast.LENGTH_SHORT).show();
        Log.d("MainActivity", "username:" );
        String id = "channel_004";
        String name = "name";
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(context.NOTIFICATION_SERVICE);

        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
            Intent intent2 = new Intent(context, Medicine.class); //点击了之后进入的一个Actity
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_ONE_SHOT);
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH  );
            mChannel.enableVibration(true);


            notificationManager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(context)
                    .setChannelId(id)
                    .setContentTitle("服药提醒")
                    .setContentText("你的好友提醒你吃药啦！")
                    .setSmallIcon(R.drawable.medicine)
                    .setContentIntent(pendingIntent)//点击之后的页面
                    .setDefaults(Notification.DEFAULT_ALL)
                    .build();
        }else{
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setContentTitle("活动")
                    .setContentText("您有项新活动")
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setOngoing(true)
                    .setChannelId(id);//无效

            notification = notificationBuilder.build();
        }

        notificationManager.notify(1,notification);
    }
}
